package com.ultrapower.request;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Scan;

import com.sematext.hbase.wd.RowKeyDistributorByHashPrefix;
import com.ultrapower.mapper.MapStatisticsCalleeMapper;
import com.ultrapower.util.ApplicationMapStatisticsUtils;
import com.ultrapower.util.HBaseTables;
import com.ultrapower.util.TimeSlot;
import com.ultrapower.util.rowkey.RangeOneByteSimpleHash;
import com.ultrapower.util.topology.LinkDataMap;

/**
 * 
 * @author wangy
 *
 */
public class ApplicationMapStatisticsCaller_Ver2Table extends BaseRequest {
	private TimeSlot timeSlot = new TimeSlot();
	private static final int SCAN_CACHE_SIZE = 40;
	private final RowKeyDistributorByHashPrefix rowKeyDistributorByHashPrefix = new RowKeyDistributorByHashPrefix(new RangeOneByteSimpleHash(0, 36, 32));
//	private final RowKeyDistributorByHashPrefix statisticsCalleeRowKeyDistributor = new RowKeyDistributorByHashPrefix(
//			new RangeOneByteSimpleHash(0, 36, 32));

	public static void main(String[] args) {
		BaseRequest table = new ApplicationMapStatisticsCaller_Ver2Table();
		table.execute();
	}

	@Override
	void execute() {
		TableName mapStatisticsCallerTableName = this.getTableName(HBaseTables.MAP_STATISTICS_CALLER_VER2_STR);
		MapStatisticsCalleeMapper map = new MapStatisticsCalleeMapper(rowKeyDistributorByHashPrefix);
		
		this.getHbaseTemplate().find(mapStatisticsCallerTableName, createScan(), rowKeyDistributorByHashPrefix,
				result -> {
					result.forEach(c->{
						try {
							LinkDataMap dataMap = map.mapRow(c, 0);
							System.out.println(dataMap);
						} catch (Exception e) {
							e.printStackTrace();
						}
					});
					return null;
				});
	}

	@Override
	protected Scan createScan() {
		final long startTime = timeSlot.getTimeSlot(from) - 1;
		final long endTime = timeSlot.getTimeSlot(to);
		this.applicationName="192.168.120.15:9200";
		this.srviceTypeCode=9052;
		byte[] startKey = ApplicationMapStatisticsUtils.makeRowKey(this.applicationName, this.srviceTypeCode, endTime);
		byte[] endKey = ApplicationMapStatisticsUtils.makeRowKey(this.applicationName, this.srviceTypeCode, startTime);

		Scan scan = new Scan();
		scan.setCaching(SCAN_CACHE_SIZE);
		scan.setStartRow(startKey);
		scan.setStopRow(endKey);
		scan.addFamily(HBaseTables.MAP_STATISTICS_CALLER_VER2_CF_COUNTER);
		scan.setId("ApplicationStatisticsScan");

		return scan;
	}

}
